<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租赁状态" prop="customerLeaseStatus">
        <el-select v-model="queryParams.customerLeaseStatus" placeholder="请选择租赁状态" clearable>
          <el-option
            v-for="dict in dict.type.customer_lease_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:customerInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:customerInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:customerInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:customerInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="60" type="index" align="center" prop="customerId" />
      <el-table-column label="客户编号" align="center" prop="customerCode" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="联系人" align="center" prop="customerLinkman" />
      <el-table-column label="联系方式" align="center" prop="customerPhone1" >
        <template slot-scope="scope">
          {{scope.row.customerPhone1}}<span v-if="scope.row.customerPhone2">/{{scope.row.customerPhone2}}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁次数" align="center" prop="customerLeaseCount" />
      <el-table-column label="租赁状态" align="center" prop="customerLeaseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_lease_status" :value="scope.row.customerLeaseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="证件" align="center" prop="">
        <template slot-scope="scope">
          <image-preview v-if="scope.row.cardPersonImg" :width="40" :height="40" :src="scope.row.cardPersonImg" style="margin-right: 10px"></image-preview>
          <image-preview v-if="scope.row.cardBehindImg" :width="40" :height="40" :src="scope.row.cardBehindImg"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            title="修改"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:customerInfo:edit']"
          ></el-button>
          <el-button
            size="small"
            type="danger"
            title="删除"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:customerInfo:remove']"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户台账管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户名称" prop="customerLinkman">
              <el-input v-model="form.customerLinkman" placeholder="客户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="身份证号" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-form-item label="地址" prop="customerAddress">
          <el-input v-model="form.customerAddress" placeholder="地址" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话1" prop="customerPhone1">
              <el-input v-model="form.customerPhone1" placeholder="联系电话1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话2" prop="customerPhone2">
              <el-input v-model="form.customerPhone2" placeholder="联系电话2" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证正面" prop="cardPersonImg">
              <ImageUpload v-model="form.cardPersonImg" :limit="1"></ImageUpload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证反面" prop="cardBehindImg">
              <ImageUpload v-model="form.cardBehindImg" :limit="1"></ImageUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomerInfo, getCustomerInfo, delCustomerInfo, addCustomerInfo, updateCustomerInfo } from "@/api/business/customerInfo";

export default {
  name: "CustomerInfo",
  dicts: ['customer_type', 'customer_lease_status'],
  data() {
    var validatePhone = (rule, value, callback) => {
      const reg = /^1[3|4|5|6|7|8|9][0-9]{9}$/
      if (!reg.test(value)) {
        callback(new Error('请输入有效的电话号码'))
      } else {
        callback()
      }
    };
    var validateIdCard = (rule, value, callback) => {
      const reg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/
      if (!reg.test(value)) {
        callback(new Error('请输入有效的身份证号'))
      } else {
        callback()
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户台账管理表格数据
      customerInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerCode: null,
        customerType: 1,
        customerName: null,
        customerLinkman: null,
        customerAddress: null,
        customerPhone1: null,
        customerPhone2: null,
        customerLeaseCount: null,
        customerLeaseStatus: null,
        customerCurrentLeaseId: null,
        customerHistoricalLeaseId: null,
        customerDate: null,
        customerIsDelete: null,
        extendedColumn1: null,
        extendedColumn2: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
          { validator: validateIdCard, trigger: 'blur' }
        ],
        customerLinkman: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        cardPersonImg: [
          { required: true, message: "请上传身份证正面照片", trigger: "blur" }
        ],
        cardBehindImg: [
          { required: true, message: "请上传身份证反面照片", trigger: "blur" }
        ],
        customerPhone1: [
          { required: true, message: "联系电话1不能为空", trigger: "blur" },
          { validator: validatePhone, trigger: 'blur' }
        ],
      }

    };

  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户台账管理列表 */
    getList() {
      this.loading = true;
      listCustomerInfo(this.queryParams).then(response => {
        this.customerInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.editFlag = false
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        customerId: null,
        customerCode: null,
        customerType: 1,
        customerName: null,
        customerLinkman: null,
        customerAddress: null,
        customerPhone1: null,
        customerPhone2: null,
        customerLeaseCount: null,
        customerLeaseStatus: null,
        customerCurrentLeaseId: null,
        customerHistoricalLeaseId: null,
        customerDate: null,
        customerIsDelete: null,
        extendedColumn1: null,
        extendedColumn2: null,
        cardPersonImg: null,
        cardBehindImg: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.editFlag = true
      const customerId = row.customerId || this.ids
      getCustomerInfo(customerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.customerId != null) {
            updateCustomerInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.customerName = this.form.customerLinkman
            addCustomerInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const customerIds = row.customerId || this.ids;
      this.$modal.confirm('是否确认删除客户台编号为"' + customerIds + '"的数据项？').then(function() {
        return delCustomerInfo(customerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/customerInfo/export', {
        ...this.queryParams
      }, `customerInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
